package myServlet;

import Smt.ServerImp.SmtUserServerImp;
import Smt.Util.SmtUser;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@SuppressWarnings("serial")
public class SmtUserUpdate extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String ids = request.getParameter("id");
		int id = 0;
		if (ids != null) {
			id = Integer.parseInt(ids);
		}
		String sex = request.getParameter("sex");
		String tel = request.getParameter("tel");
		String ages = request.getParameter("age");
		int age = 0;
		if (ages != null) {
			age = Integer.parseInt(ages);
		}
		String password = request.getParameter("password");
		String add = request.getParameter("adds");
		String rms = request.getParameter("rms");
		String name = request.getParameter("name");
		SmtUser s = new SmtUser(id, name, sex, age, tel, add, rms, password);
		SmtUserServerImp susi = new SmtUserServerImp();

		String loginname = (String) request.getSession().getAttribute("loginname");
		String admin = (String) request.getSession().getAttribute("admin");
		if (name.equals(loginname) && rms.equals("user") && "admin".equals(admin)) {
			susi.update(s);
			request.getSession().invalidate();
			request.getRequestDispatcher("SmtUserRead").forward(request, response);
		} else if (susi.update(s)) {
			int nums1 = Integer.parseInt(request.getParameter("num"));
			request.setAttribute("num", nums1);
			request.getRequestDispatcher("SmtUserRead").forward(request, response);
		} else {
			request.setAttribute("error", "更新信息有误，我也不知道是什么问题！");
			response.sendRedirect("SmtUserRead");
		}
	}
}
